package com.ztom.daily.n2022.m3;

/**
 * 交替位二进制数
 * <p>
 * https://leetcode-cn.com/problems/binary-number-with-alternating-bits/
 *
 * @author ZhangTao
 */
public class Day28HasAlternatingBits {

    public boolean hasAlternatingBits(int n) {
        // 01010101 >> 1 = 00101010
        // 01010101 ^ 00101010 = 01111111
        // 01111111 & (01111111 + 1) = 0
        int x = n ^ (n >> 1);
        return (x & (x + 1)) == 0;
    }
}
